package com.dyna.controller;

import com.dyna.domain.form.item.ItemsForm;
import com.dyna.domain.req.ItemFlashPurchaseReq;
import com.dyna.domain.req.ItemPageReq;
import com.dyna.domain.resp.PageResult;
import com.dyna.domain.resp.R;
import com.dyna.domain.resp.item.ItemPageResp;
import com.dyna.service.ItemsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @description items
 * @author zcw
 * @date 2025-02-27
 */
@Api("千岛/物品管理")
@RestController
@RequestMapping(value = "/items")
public class ItemsController {

    private final ItemsService itemsService;

    @Autowired
    public ItemsController(ItemsService itemsService) {
        this.itemsService = itemsService;
    }

    @ApiOperation("物品分页列表")
    @PostMapping("/list")
    public R<PageResult<ItemPageResp>> list(@RequestBody @Validated ItemPageReq req){
        return R.data(itemsService.pageList(req));
    }

    @ApiOperation("新增物品")
    @PostMapping("/add")
    public R<Boolean> add(@RequestBody ItemsForm form){
        return R.isSuccess(itemsService.add(form));
    }

    @ApiOperation("删除商品")
    @GetMapping("/del/{itemId}")
    public R<Boolean> del(@PathVariable Long itemId) {
        return R.isSuccess(itemsService.del(itemId));
    }

    @ApiOperation("闪购物品")
    @PostMapping("/flash/purchase")
    public R<Boolean> purchase(@RequestBody @Validated ItemFlashPurchaseReq req) {
        return R.isSuccess(itemsService.purchase(req));
    }

}